'''
Created on Aug 13, 2012

@author: peng
'''
import re
import os
import ftplib
import datetime

ftp = ftplib.FTP("119.254.86.77")
print ftp.login("conch", "Conpass@w0rdch!")

log_path = '/var/hadoop/input'


def get_folder(file_name):
    match = re.search(r'(\d+\d+\d+)', file_name)
    if match:
        time_str = match.group(1)
        folder_name = datetime.datetime.strptime(time_str, '%Y%m%d')
        return folder_name.strftime('%Y-%m-%d')
    return None


def upload(file_path):
    file_name = os.path.basename(file_path)
    folder_name = get_folder(file_name)
    if folder_name:
        ftp.mkd(folder_name)
        ftp.storbinary('STOR %s/%s' % 
                       (folder_name, file_name), open(file_path, 'rb'))


def get_files():
    time_str = datetime.datetime.now().strftime('%Y-%m-%d')
    source_folder = os.path.join(log_path, time_str, 'dv')
    if os.path.exists(source_folder):
        files = os.listdir(source_folder)
        dv_files = filter(lambda x: x.startswith('dv.log'), files)
        return [os.path.join(source_folder, f) for f in dv_files]
    return None


def get_file():
    time_str = datetime.datetime.now().strftime('%Y%m%d')
    file_name = 'dv.log-%s' % time_str
    file_path = os.path.join(log_path, file_name)
    if os.path.exists(file_path):
        return file_path
    return None


def ftp_files():
    current_dir = os.path.dirname(os.path.abspath(__file__))
    f = open(os.path.join(current_dir, 'files'))
    for l in f:
        upload(l.replace('\n', ''))

if __name__ == '__main__':
    try:
        file_paths = get_files()
        for file_path in file_paths:
            upload(file_path)
            print '[FTP][%s]' % file_path
    except Exception, e:
            print '[Exception][%s]' % unicode(e)
